package com.CGFrame.struts.action;

import java.io.File;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.CGFrame.struts.bean.DemoBean;
import com.CGFrame.struts.bean.GenerateReportBean;

public class ShowJasperreportAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String path="/tempReport";
		String jrxmlFile=getServlet().getServletContext().getRealPath(File.separator)+"tempReport"+File.separator+"BeanArray.jrxml";
		String format=request.getParameter("format");
		
		//在服务器文件系统生成文件,再访问
/**
		if(format!=null && !format.equals("") && format.length()!=0){
			String fileName=null;
			Map parameters=new HashMap();
			//作为数据源的bean数组
			DemoBean[] beans=DemoBean.getBeans();
			if(format.equals("PDF")){
				fileName=GenerateReportBean.generatePDFFileByBeanArray(jrxmlFile,parameters,beans);
				response.setContentType("application/pdf");
			}else if(format.equals("excel")){
				fileName=GenerateReportBean.generateExcelFileByBeanArray(jrxmlFile,parameters,beans);
				response.setContentType("application/vnd.ms-excel");
			}else if(format.equals("html")){
				fileName=GenerateReportBean.generateHTMLFileByBeanArray(jrxmlFile,parameters,beans);
				response.setContentType("text/html");
			}
			ActionForward af=new ActionForward(path+fileName);
			
			return af;
		}
*/
		//把文件作为byte[]放入reaponse.getOutputStream()
		if(format!=null && !format.equals("") && format.length()!=0){
			byte[] bytes=null;
			Map parameters=new HashMap();
			GregorianCalendar beginDate=new GregorianCalendar(2009,1,1);
			GregorianCalendar endDate=new GregorianCalendar(2009,1,30);
			parameters.put("begin", beginDate);
			parameters.put("end", endDate);
			//作为数据源的bean数组
			DemoBean[] beans=DemoBean.getBeans();
			if(format.equals("PDF")){
				bytes=GenerateReportBean.generatePDFByBeanArray(jrxmlFile,parameters,beans);
//				response.setContentType("application/pdf");
			}else if(format.equals("excel")){
				bytes=GenerateReportBean.generateExcelByBeanArray(jrxmlFile,parameters,beans);
//				response.setContentType("application/vnd.ms-excel");
			}else if(format.equals("html")){
				bytes=GenerateReportBean.generateHTMLByBeanArray(jrxmlFile,parameters,beans);
//				response.setContentType("text/html");
			}
			request.setAttribute("output", bytes);
			return mapping.findForward("output");
		}
		return null;
	}
}
